/**
 * 返回非负数字x中从右开始p位置的n个比特
 */

#include <stdio.h>

unsigned getbits(unsigned x, int p, int n);

int main() {
    unsigned x = 255;
    int p = 7;
    int n = 8;
    printf("0x%x\n", getbits(x, p, n));

    return 0;
}

unsigned getbits(unsigned x, int p, int n) {
    return (x >> (p - n + 1)) & ~(~0 << n);
}
